package com.james.wee.shuangseqiu.hechu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.james.wee.shuangseqiu.bo.IDBBo;
import com.james.wee.shuangseqiu.bo.OpenNumber;
import com.james.wee.shuangseqiu.db.DB;

public class HeCHu {

	public static void hechus() throws Exception {
		List<IDBBo> list = DB
				.getInstance()
				.queryBoList(
						"select * from OpenNumber where opennum>'2012000' order by id asc",
						OpenNumber.class);
		OpenNumber on = null;
		OpenNumber next = null;
		String nt ="";
		for (int i=0,len=list.size();i<len;i++) {
			on = (OpenNumber) list.get(i);
			if(i<=len-2){
				next=(OpenNumber) list.get(i+1);
				nt = next.sixRedNum();
			}
			System.out.println((Integer.parseInt(on.getOpennum()) + 1) + ":"
					+ getnum(on.sixRedNum())+"==>"+nt);
			nt ="";
		}
	}

	private static int getTotal(String str) {
		int total = 0;
		String[] ses = str.split(",");
		for (String s : ses) {
			total += Integer.parseInt(s);
		}
		return total;
	}

	private static String getnum(String str) {
		int total = getTotal(str);
		String[] ses = str.split(",");
		int s = 0;
		String res = "";
		String dou ="";
		Map<Integer,Integer> map = new HashMap<Integer, Integer>();
		for (String sa : ses) {
			s = (total / Integer.parseInt(sa));
			if (s >= 10 && s < 20) {
				s = s % 10;
			} else if (s >= 20 && s < 30) {
				s = s % 20;
			} else if (s >= 30) {
				s = s % 30;
			}
			map.put(s, s);
		}
		for(java.util.Iterator<Integer> it = map.keySet().iterator();it.hasNext();){
			s= it.next();
		String k = ""+((s + 30) <= 33 ? (s + 30) : "");
		res += dou+ s + "," + (s + 10) + "," + (s + 20)  ;
		if(k.length()>0)
			res+=","+k;
		
		dou = ",";
		}
		String[] r = res.split(",");
		Map<String,String> maps= new HashMap<String,String>();
		for(String a : r)
			maps.put(a,a);
		java.util.Set<String> c = maps.keySet();
		List<Integer> list = new 	ArrayList<Integer>();
		for(java.util.Iterator<String> it = c.iterator();it.hasNext();)
			list.add(Integer.parseInt(it.next()));
		java.util.Collections.sort(list);
		res = list.toString();
		return res.substring(1,res.length()-1);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			hechus();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
